$.extend($.fn,{
    createProcess:function(options){
        var content = $("<div class='process-content'></div>").appendTo($(this));
        var fText = $('<span>0%</span>').appendTo(content);
        var processContent = $('<div></div>').appendTo(content);
        var tText = $('<span>0%</span>').appendTo(processContent);

        options = options||{};

        var processAnimate;

        var timeUpdater;

        var Handler = function(){
            this.width = 300;
            this.totalVal = 1;
            this.val = 0;
            this.loadingText = "加载中...";
			this.spacing=0;
			this.alignText="right";
            this.setWidth = function(w){
                this.width = w;
                if(typeof(w) === 'string' && w.indexOf("%") === w.length-1){
                    content.css('width',w);
                }else{
                    content.width(w);
                }
                this.setVal(this.val);
            };
            this.setHeight = function(h){
                content.height(h);
                content.css('line-height',content.height()+'px');
            };
			this.setSpacing = function(space){
				this.spacing=space;
				processContent.css("top",space+"px");
				processContent.css("left",space+"px");
				processContent.css("bottom",space+"px");
				tText.css("top",-space+"px");
                fText.css("left",'');
                fText.css("right",'');
				if(this.alignText === 'left'){
                    fText.css("left",this.spacing+"px");
                }else if(this.alignText === 'right'){
                    fText.css("right",this.spacing+"px");
                }
				this.refresh();
			}
			this.setAlignText = function(align){
				align = align||"right";
				this.alignText = align;
				if(align==='right'){
					fText.css("right",this.spacing+"px");
					fText.css("left",'');
					fText.css("text-align",'');
					tText.css("text-align","");
				}else if(align==='left'){
					fText.css("right",'');
					fText.css("left",this.spacing+"px");
					fText.css("text-align",'');
					tText.css("text-align","");
				}else if(align==='center'){
					fText.css("right",this.spacing+"px");
					fText.css("left",this.spacing+"px");
					fText.css("text-align","center");
					tText.css("text-align","center");
				}
				this.refresh();
				
			};
            this.setTotalVal=function(value){
                value=value<=0?1:value;
                this.totalVal = value;
                this.setVal(this.val);
            };
            this.setVal=function(value){
				if(value != -1){
					value = value<0?0:value;
                	value = value>this.totalVal?this.totalVal:value;
				}
                this.val = value;
                if(processAnimate){
                    window.clearInterval(processAnimate);
                    processAnimate = undefined;
                    processContent.css('left','0px');
                }
                if(value == -1){
                    processContent.css('width','20%');
                    fText.text(this.loadingText);
                    tText.text(this.loadingText);
                    var self = this;
                    var de = true;
                    processAnimate = window.setInterval(function(){
                        var left = processContent.position().left;
                        var width = processContent.width();
                        var tWidth = content.width()-self.spacing*2;
                        if(de){
                            left+=2;
                            if(left+width > tWidth){
                                left = tWidth - width;
                                de=false;
                            }
                        }else{
                            left -= 2;
                            if(left<0){
                                left=0;
                                de=true;
                            }
                        }
                        processContent.css('left',left+'px');
                        self.refresh();
                    },20);
                    return;
                }

                value = value>this.totalVal?this.totalVal:value;
                var v = this.val / this.totalVal;
                fText.text((v*100).toFixed(2)+"%");
                tText.text(fText.text());
                processContent.width(parseInt(v*(content.width()-this.spacing*2)));
                this.refresh();
            };
            this.setBgColor=function(color){
                content.css('background-color',color);
                tText.css('color',color);
            };
            this.setProcessColor=function(color,b){
				if(b === true){
					this.setBorderColor(color);
				}
                processContent.css('background-color',color);
                fText.css('color',color);
            };
			this.setBorderColor = function(color){
				content.css('border-color',color);
			};
            this.refresh=function(){
                tText.offset({left:fText.offset().left});
                tText.width(fText.width())
                tText.height(fText.height())
            };
            this.destroy = function(){
                this.destroyProcess();
                content.remove();
            };
            this.disableText = function(){
                tText.hide();
                fText.hide();
            };
            this.enableText = function(){
                tText.css('display','inline-block');
                fText.css('display','inline-block');
            };
            this.setVisible = function(v){
                if(v === true){
                    content.show();
                }else if(v === false){
                    content.hide();
                }
            };
            this.setLoadingText = function(text){
                this.loadingText = text||"加载中...";
                if(processAnimate){
                    fText.text(this.loadingText);
                    tText.text(this.loadingText);
                }
            }

           //p:更新进度方法 t更新间隔时间 d是否结束后自动销毁更新。
            this.beginProcess = function(p,t,d){
               if(typeof(p) === "function"){
                  if(typeof(t)==="boolean"){
                     p=t;
                     t=1000;
                  }
                  t=t||1000;
                  t=isNaN(t)?1000:t;
                  this.destroyProcess();
				  var f=p;
				  var self = this;
				  var v;
				  if(d === true){
					 p = function(){
					  v = f();
					  if(!isNaN(v)){
						  self.setVal(v);
					  }
					  if(self.totalVal === self.val){
						  self.destroyProcess();
					  }
				  	} 
				  }else{
					  p = function(){
					  v = f();
					  if(!isNaN(v)){
						  self.setVal(v);
					  }
				  	} 
				  }
                  timeUpdater = window.setInterval(p,t);
               }
            }

           this.destroyProcess = function(){
              if(timeUpdater){
                     window.clearInterval(timeUpdater);
                     timeUpdater = undefined;
                  }
           }

            return this;
        }

        var h = new Handler();

        if(options.bgColor){
            h.setBgColor(options.bgColor);
        }
        if(options.processColor){
            h.setProcessColor(options.processColor);
        }
		if(options.borderColor){
			h.setBorderColor(options.borderColor);
		}
		if(options.space){
			h.setSpacing(options.space);
		}
        if(options.width){
            h.setWidth(options.width);
        }
        if(options.height){
            h.setHeight(options.height);
        }
        if(options.total){
            h.setTotalVal(options.total);
        }
        if(options.val){
            h.setVal(options.val);
        }
        if(options.enableText===false){
            h.disableText();
        }
        if(options.loadingText){
            h.setLoadingText(options.loadingText);
        }
		if(options.alignText){
			h.setAlignText(options.alignText);
		}

        return h;
    }
})